﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SingletonPattern.Presenter;
using SingletonPattern.Model;
using SingletonPattern.utils;
using System.Reflection;
using System.Collections;

namespace SingletonPattern.View
{
    public partial class UserView : Form, IUserView
    {
        private UserPresenter m_userPresenter;
        private Dictionary<TextBox, List<string>> m_lsTexbox;
        public UserView()
        {
            InitializeComponent();
            m_lsTexbox = new Dictionary<TextBox, List<string>>();

            m_lsTexbox.Add(name, new List<string>() { "string" }); // not allow null or empty
            m_lsTexbox.Add(age, new List<string>() { "int", "", "31" });
            m_lsTexbox.Add(nationality, new List<string>() { "string", ""}); // allow null or empty
            m_lsTexbox.Add(club, new List<string>() { "string", "0123456789ABCDEF" }); // limit characters
            m_userPresenter = new UserPresenter(new UserModel(), this);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            foreach (KeyValuePair<TextBox, List<string>> entry in m_lsTexbox)
            {
                try
                {
                    string type = entry.Value[0].ToString();
                    if (type == "string")
                    {
                        if ((entry.Value).Count == 1 && SingletonPattern.utils.String.isNullEmpty(entry.Key.Text))
                            throw new SingletonPattern.utils.InvalidString(entry.Key.Name);
                        if ((entry.Value).Count == 2 && !SingletonPattern.utils.String.isNullEmpty(entry.Value[1]))
                        {
                            for (int i = 0; i < entry.Key.Text.Length; i++)
                            {
                                if (!entry.Value[1].Contains(entry.Key.Text[i]))
                                    throw new SingletonPattern.utils.InvalidLimitString(entry.Value[1] + " (field " + entry.Key.Name + " )");
                            }
                        }
                    }
                    else if (type == "int")
                    {
                        int min = SingletonPattern.utils.String.isNullEmpty((entry.Value)[1].ToString())
                            ? int.MinValue : Int32.Parse((entry.Value)[1].ToString());
                        int max = SingletonPattern.utils.String.isNullEmpty((entry.Value)[2].ToString())
                                ? int.MinValue : Int32.Parse((entry.Value)[2].ToString());
                        if (!SingletonPattern.utils.Number.isNumberBounded(entry.Key.Text, min, max))
                            throw new SingletonPattern.utils.InvalidNumber(entry.Key.Name);
                    }
                }
                catch (SingletonPattern.utils.InvalidString ex)
                {
                    ex.InvalidStringException();
                    return;
                }
                catch (SingletonPattern.utils.InvalidNumber ex)
                {
                    ex.InvalidNumberException();
                    return;
                }
                catch (SingletonPattern.utils.InvalidLimitString ex)
                {
                    ex.InvalidLimitStringException();
                    return;
                }
                CurrentSetting.Instance[entry.Key.Name] = entry.Key.Text;
            }
            m_userPresenter.setUser();
            MessageBox.Show("Success");
        }

        public void loadUser()
        {
            foreach (TextBox tb in m_lsTexbox.Keys)
            {
                tb.Text = CurrentSetting.Instance[tb.Name].ToString();
            }
        }

    }
}
